package com.itheima.controller;

import com.itheima.pojo.Book;
import com.itheima.pojo.BorrowRecord;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.vo.BookVo;
import com.itheima.pojo.vo.BorrowRecordVo;
import com.itheima.service.BorrowRecordService;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/record")
public class BorrowRecordController {
    @Autowired
    private BorrowRecordService borrowRecordService;
    /**
     *新增记录
     */
    @PostMapping
    public Result add(@RequestBody @Validated BorrowRecord borrowRecord) {
        borrowRecordService.add(borrowRecord);
        return Result.success();
    }

    /**
     *分页查询记录
     */
    @GetMapping
    public Result<PageBean<BorrowRecordVo>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String cardNumber,
            @RequestParam(required = false) Integer bookId,
            @RequestParam(required = false) Integer status
    ) {
        PageBean<BorrowRecordVo> pb =  borrowRecordService.list(pageNum,pageSize,cardNumber,bookId,status);
        return Result.success(pb);
    }
    /**
     * 更新记录
     */
    @PutMapping
    public Result update(@RequestBody BorrowRecord borrowRecord){
        int a=1;
        borrowRecordService.update(borrowRecord);
        return Result.success();
    }

    /**
     * 删除记录
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        borrowRecordService.delete(id);
        return Result.success();
    }
    /**
     * 更改状态为归还
     */
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer id){
        borrowRecordService.updateStatus(id);
        return Result.success();
    }

}
